const app = getApp()
const constant = require("../../../../constant.js")
const storage = require("../../../../utils/storage.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      host: constant.api(app.globalData.pord).url,
      mobile: wx.getStorageSync(storage.keys.mobile),
      courseRoundId: options.courseRoundId
    })

    if (options.tagIds) {
      this.setData({
        tagIds: JSON.parse(options.tagIds)
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.showLoading({
      title: "正在加载中..."
    })

    var that = this;

    var url = that.data.host + '/coach/course-rounds/' + that.data.courseRoundId + '/student-tags?mobile=' + that.data.mobile;
    if(that.data.tagIds) {
      for (var i = 0; i < that.data.tagIds.length; i++) {
        url = url + '&tagIds=' + that.data.tagIds[i];
      }
    }

    wx.request({
      url: url,
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        if (res.data.code == "0") {
          that.setData({
            tags: res.data.data.tags
          });

          wx.hideLoading();
        } else {
          wx.hideLoading();

          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 1000
          })
        }
      },
      fail: function (res) {
        wx.hideLoading();

        wx.showToast({
          title: '请求数据失败',
          icon: 'none',
          duration: 1000
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  bindCancel: function() {
    wx.navigateBack({
      delta: 1
    })
  },

  bindSubmit: function() {
    // 处理数据返回数据
    let array = this.data.tags
    var selectedTagNames= '';
    let selectedTagIds = [];
    for (let i in array) {
      if (array[i].selected) {
        selectedTagNames = selectedTagNames + array[i].name + ','
        selectedTagIds.push(array[i].id)
      }
    }

    const wxCurrPage = getCurrentPages();
    const wxPrevPage = wxCurrPage[wxCurrPage.length - 2];
    if (wxPrevPage) {
      wxPrevPage.setData({
        tags: selectedTagNames.length > 0 ? selectedTagNames.substring(0, selectedTagNames.length - 1) : '',
        tagIds: selectedTagIds,
        tagIdParas: '&tagIds=' + JSON.stringify(selectedTagIds),
        loadTag: false
      })
    }

    wx.navigateBack({
      delta: 1
    })
  },

  bindsSelectedTags: function(e) {
    let tags = "tags[" + e.currentTarget.id + "].selected"
    this.setData({
      [tags]: !this.data.tags[e.currentTarget.id].selected
    })

  }

})